﻿namespace MicroCloud.EventBuses
{
    /// <summary>
    /// 事件处理器接口
    /// <para>所有事件处理都要实现该接口</para>
    /// <para>EventBus中，Handler的调用是同步执行的，如果需要触发就不管的异步执行，可以在实现EventHandler的Handle逻辑时使用Task.Run。</para>
    /// </summary>
    [IgnoreDependency]
    public interface IEventHandler
    {
        /// <summary>
        /// 是否可处理指定事件
        /// </summary>
        /// <param name="eventData">事件源数据</param>
        /// <param name="provider">服务提供者</param>
        /// <returns>是否可处理</returns>
        bool CanHandle(IEventData eventData, IServiceProvider provider);

        /// <summary>
        /// 事件处理
        /// </summary>
        /// <param name="eventData">事件源数据</param>
        /// <param name="provider">服务提供者</param>
        void Handle(IEventData eventData, IServiceProvider provider);

        /// <summary>
        /// 异步事件处理
        /// </summary>
        /// <param name="eventData">事件源数据</param>
        /// <param name="provider">服务提供者</param>
        /// <param name="cancelToken">异步取消标识</param>
        /// <returns></returns>
        Task HandleAsync(IEventData eventData, IServiceProvider provider, CancellationToken cancelToken = default);
    }

    /// <summary>
    /// 泛型事件处理器接口
    /// <para>EventBus中，Handler的调用是同步执行的，如果需要触发就不管的异步执行，可以在实现EventHandler的Handle逻辑时使用Task.Run。</para>
    /// </summary>
    /// <typeparam name="TEventData">事件源数据</typeparam>
    [IgnoreDependency]
    public interface IEventHandler<in TEventData> : IEventHandler where TEventData : IEventData
    {
        /// <summary>
        /// 事件处理
        /// </summary>
        /// <param name="eventData">事件源数据</param>
        /// <param name="provider">服务提供者</param>
        void Handle(TEventData eventData, IServiceProvider provider);

        /// <summary>
        /// 异步事件处理
        /// </summary>
        /// <param name="eventData">事件源数据</param>
        /// <param name="provider">服务提供者</param>
        /// <param name="cancelToken">异步取消标识</param>
        Task HandleAsync(TEventData eventData, IServiceProvider provider, CancellationToken cancelToken = default);
    }
}